#include "stdafx.h"
#include "GameStateMgr.h"
#include "factory.h"
#include "GameAnalyze.h"
#include "WowFun.h"
#include "RunSafe.h"
#include "SpellMgr.h"
#include "DBDataMgr.h"

CRunSafe::CRunSafe():m_ListPos(GET_DB().m_vGoBackPath)
{

}

void CRunSafe::Reset()
{
    GetBestPos();
}

void CRunSafe::DoAction()
{
    CPlayer& Player = *IFactory::getSingleton().GetPlayer();
    if (Player.IsCasting() || Player.IsChannel()) 
    {
        sMyTime::Instance().Sleep(800);
        return;
    }

    DWORD dz = Player.MoveToPos(m_sNextPosition, E_FORWARD|E_ARRIVE_NO_STOP|E_HARD_BLOCK, 2.6f);
    if (dz==1)
    {
        m_sNextPosition = GetNextPos();
    }
}

sPos CRunSafe::GetNextPos()
{
    CPlayer &player = *IFactory::getSingleton().GetPlayer();
    if (m_ListPos.size()==0)
    {
        return player.GetPos();
    }

    m_dwNextPos = (m_dwNextPos+1)%m_ListPos.size();
    return m_ListPos[m_dwNextPos];
}

sPos CRunSafe::GetBestPos()
{
    CPlayer &player = *IFactory::getSingleton().GetPlayer();
    sPos posNow = player.GetPos();
    if (m_ListPos.size()==0)
    {
        return posNow;
    }

    m_dwNextPos = 0;
    float dis = 10000.0f;
    for (size_t i=0; i < m_ListPos.size(); ++i)
    {
        float ff = CalcDistance2D(m_ListPos[i],posNow);
        if (ff < dis)
        {
            dis = ff;
            m_dwNextPos = i;
        }
    }
    return m_ListPos[m_dwNextPos];
}